package com.zqll.gdx.starter.assets;

import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.backends.lwjgl3.Lwjgl3Application;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator.FreeTypeFontParameter;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.ui.Table;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
import com.badlogic.gdx.utils.viewport.ScreenViewport;

/**
 * OTF字体格式测试示例
 * 演示如何使用OTF格式的字体文件
 */
public class OTFFontExample extends ApplicationAdapter {

    public static void main(String[] args) {
        new Lwjgl3Application(new OTFFontExample());
    }

    private SpriteBatch batch;
    private Stage stage;
    private Skin skin;
    private BitmapFont incrementalFont;
    private BitmapFont infoFont;
    
    @Override
    public void create() {
        batch = new SpriteBatch();
        stage = new Stage(new ScreenViewport());
        
        // 初始化统一入口
        StarterAssets.init("assets/" + AssetPaths.FONT_NOTO_MONO_CJK);

        // 创建皮肤（可直接使用 StarterAssets 的皮肤，避免样式缺失）
        skin = new Skin();
        
        // 为皮肤添加指定 OTF 字体
        FreeTypeFontParameter p = new FreeTypeFontParameter();
        p.size = 24;
        p.incremental = true;
        String fontPath = "assets/fonts/NotoSansMonoCJKsc-Regular.otf";
        fontPath = "assets/" + AssetPaths.FONT_MAPLE_MONO_SC;
        fontPath = "assets/" + AssetPaths.FONT_NOTO_MONO_CJK;
        BitmapFont otfFont = StarterAssets.getFontGenerator().generateFont(p, fontPath);
        com.zqll.gdx.starter.assets.utils.SkinFonts.addFont(skin, otfFont, "otf-font");

        // 为特定文本生成增量字体
        String specificText = "OTF字体测试：性能与兼容性";
        FreeTypeFontParameter ip = new FreeTypeFontParameter();
        ip.size = 20;
        ip.color = Color.WHITE;
        ip.incremental = true;
        // BitmapFont incOtfFont = StarterAssets.getFontGenerator().generateFont(ip, fontPath);
        // com.zqll.gdx.starter.assets.utils.SkinFonts.addFont(skin, incOtfFont, "incremental-otf-font");

        StarterAssets.init(fontPath, 16);
        BitmapFont chineseFont = StarterAssets.font(20);
        skin.add("incremental-otf-font", chineseFont);

        // 创建UI组件
        createUI();
        
        // 生成信息显示字体
        infoFont = StarterAssets.font(16, Color.CYAN);
        
        Gdx.input.setInputProcessor(stage);
    }
    
    private void createUI() {
        Table table = new Table();
        table.setFillParent(true);
        
        // 创建标签 - 使用OTF字体（显式构造 LabelStyle，避免从 Skin 查找不存在的样式名）
        Label.LabelStyle labelStyle = new Label.LabelStyle();
        labelStyle.font = skin.getFont("otf-font");
        labelStyle.fontColor = Color.YELLOW;
        Label titleLabel = new Label("OTF字体格式测试", labelStyle);
        
        // 创建按钮 - 使用增量OTF字体
        TextButton.TextButtonStyle buttonStyle = new TextButton.TextButtonStyle();
        buttonStyle.font = skin.getFont("incremental-otf-font");
        buttonStyle.fontColor = Color.GREEN;
        
        TextButton testButton = new TextButton("OTF字体测试按钮", buttonStyle);
        
        // 布局
        table.add(titleLabel).padBottom(20).row();
        table.add(testButton).padBottom(20).row();
        
        // 添加多行文本测试
        String[] testTexts = {
            "OTF格式支持测试",
            "中文字符渲染效果",
            "性能与兼容性验证",
            "增量字体生成测试"
        };
        
        for (String text : testTexts) {
            Label.LabelStyle textLabelStyle = new Label.LabelStyle();
            textLabelStyle.font = skin.getFont("otf-font");
            textLabelStyle.fontColor = Color.LIGHT_GRAY;
            Label textLabel = new Label(text, textLabelStyle);
            table.add(textLabel).padBottom(10).row();
        }
        
        stage.addActor(table);
    }
    
    @Override
    public void render() {
        Gdx.gl.glClearColor(0.1f, 0.1f, 0.2f, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
        
        stage.act();
        stage.draw();
        
        // 直接渲染测试
        batch.begin();
        
        // 生成并渲染直接文本（如果还未生成）
        if (incrementalFont == null) {
            String directText = "OTF直接渲染：字体质量与性能！";
            FreeTypeFontParameter rp = new FreeTypeFontParameter();
            rp.size = 18;
            rp.color = Color.ORANGE;
            rp.incremental = true;
            incrementalFont = StarterAssets.getFontGenerator().generateFont(rp, "assets/fonts/NotoSansMonoCJKsc-Regular.otf");
        }
        
        // 使用TextUtils进行文本渲染
        String renderText = "OTF直接渲染：字体质量与性能！";
        // TextUtils.drawText(batch, incrementalFont, renderText, 50, 150, Color.ORANGE);
        
        // 测试文本测量功能
        // float textWidth = TextUtils.getTextWidth(incrementalFont, renderText);
        // float textHeight = TextUtils.getTextHeight(incrementalFont, renderText);
        //
        // String measureText = String.format("文本尺寸: %.1f x %.1f", textWidth, textHeight);
        // TextUtils.drawText(batch, infoFont, measureText, 50, 120, Color.CYAN);
        
        // 显示字符统计信息
        // Gdx.app.log("OTFFontExample", "字体生成器已初始化");
        // TextUtils.drawText(batch, infoFont, "OTF字体测试", 10, 20, Color.GREEN);
        
        batch.end();
    }
    
    @Override
    public void dispose() {
        batch.dispose();
        stage.dispose();
        skin.dispose();
        StarterAssets.dispose();
    }
}